package com.fancy.learncenter.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.AddDiscussDataBean;
import com.fancy.learncenter.bean.FFlibraryStoryBean;
import com.fancy.learncenter.common.AudioPlayer;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.MallCommonH5Activity;
import com.fancy.learncenter.ui.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.ui.adapter.base.CustomViewHold;
import com.fancy.learncenter.ui.view.SharePopupWindow;
import com.fancy.learncenter.utils.DataUtil;
import com.fancy.learncenter.utils.ImgFilter;
import com.fancy.learncenter.utils.StrUtil;
import com.fancy.learncenter.utils.Utils;
import com.fancy.learncenter.utils.WXShareUtil;

import java.util.HashMap;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;
import rx.Subscriber;

/**
 * Created by admin on 2017/4/19.
 */

public class GoldLecturePersonalAdapter extends CommonRecycleViewAdapter<FFlibraryStoryBean.ResultBean> {
    Activity context;
    boolean isPlay;
    public AudioPlayer audioPlayer;
    String nickName;
    List<FFlibraryStoryBean.ResultBean> itemDatas;
    public GoldLecturePersonalAdapter(Activity mContext, String nickName, List<FFlibraryStoryBean.ResultBean> itemDatas) {
        super(mContext, R.layout.gold_lecture_personal_item, itemDatas);
        this.context = mContext;
        this.nickName = nickName;
        this.itemDatas = itemDatas;
    }

    @Override
    public void bindView(final CustomViewHold customViewHold, final FFlibraryStoryBean.ResultBean item, final int position) {

        final ImageView audioPlay = customViewHold.getView(R.id.audio_image);
        final ImageView playImage = customViewHold.getView(R.id.audio_play_image);
        TextView title = customViewHold.getView(R.id.title);
        SimpleDraweeView simpleDraweeView = customViewHold.getView(R.id.simpleDraweeView);
        TextView playTimes = customViewHold.getView(R.id.play_times);
        TextView audioPlayDuration = customViewHold.getView(R.id.audio_play_duration);
        TextView share = customViewHold.getView(R.id.share);

//        ／
//        if (item.getTitle().length() >= 14) {
//            String bookName = item.getTitle().substring(0, 14) + "...";
//            title.setText(bookName);
//        } else {
            title.setText(item.getTitle());
//        }
        if (item.isCanPlay()){
            playImage.setVisibility(View.VISIBLE);
            audioPlay.setImageResource(R.mipmap.gold_lecture_audio_pause);
        }else{
            playImage.setVisibility(View.GONE);
            audioPlay.setImageResource(R.mipmap.gold_lecture_audio_play);
        }
        String audioTime = DataUtil.formatPlayTime(Long.valueOf(item.getRecordingDate() * 1000));
        playTimes.setText("播放" + StrUtil.formatPlayNum(item.getPlayCount()) + "次");
        simpleDraweeView.setImageURI(Utils.getImgUri(item.getBookImg() + ImgFilter.SSCC_H350_W280));
        audioPlayDuration.setText("时长" + audioTime);


        audioPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.getTaletelingPath() != null && item.getTaletelingPath().size() > 0) {
                    playAudioItem(position, item.getTaletelingPath().get(0));
                }

//                if(item.isPlayStatus()){
//                    if (audioPlayer.mediaPlayer.isPlaying()){
//                        playImage.setVisibility(View.VISIBLE);
//                        audioPlay.setImageResource(R.mipmap.gold_lecture_audio_pause);
//                        audioPlayer.pause();
//                    }else{
//                        playImage.setVisibility(View.GONE);
//                        audioPlay.setImageResource(R.mipmap.gold_lecture_audio_play);
//                        audioPlayer.replay();
//                    }
//                }else {
//                    item.setPlayStatus(true);
//                        for (int i = 0; i < itemDatas.size(); i++){
//                            if (i != position){
//                                itemDatas.get(i).setPlayStatus(false);
//                            }
//                        }
//                    audioPlayer.setDataSource(Constant.FANCY_VIDEO_URL + item.getTaletelingPath().get(0));
//                    audioPlayer.play();
//                    notifyDataSetChanged();
//
//                }
            }
        });

        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareAudio(item, customViewHold.getRoorView());
            }
        });

    }


    public void playAudioItem(final int position, String playUrl) {
        if (!itemDatas.get(position).isCanPlay()) {
            if (audioPlayer == null) {
                audioPlayer = new AudioPlayer(context.getApplicationContext(), Constant.FANCY_VIDEO_URL + playUrl);
                audioPlayer.play();
                audioPlayer.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        audioPlayer.pause();
                        refreshVolumeView(position, false, false);
                    }
                });
            } else {
                audioPlayer.setDataSource(Constant.FANCY_VIDEO_URL + playUrl);
                audioPlayer.play();
            }
            refreshVolumeView(position, true, true);
        } else {
            stopStoryRadio(position);
        }
    }

    private void stopStoryRadio(int position) {
        if (audioPlayer != null) {
            audioPlayer.pause();
            refreshVolumeView(position, false, false);
        }
    }


    private void refreshVolumeView(int position, boolean isVolumeVisible, boolean isCanPlay) {
        int length = itemDatas.size();
        for (int i = 0; i < length; i++) {
            itemDatas.get(i).setCanPlay(false);
            itemDatas.get(i).setVolumeVisible(false);
        }
        itemDatas.get(position).setCanPlay(isVolumeVisible);
        itemDatas.get(position).setVolumeVisible(isCanPlay);
        if (isCanPlay) {
            addStoryRadioSetPlayCount(itemDatas.get(position).getSysNo());
            itemDatas.get(position).setPlayCount(itemDatas.get(position).getPlayCount() + 1);
        }
        notifyDataSetChanged();
    }

    private void addStoryRadioSetPlayCount(int storyRadioNo) {
        String currentTime = String.valueOf(Utils.getCurrentTimeMillis());
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("time", currentTime);
        hashMap.put("SysNo", String.valueOf(storyRadioNo));
        HttpMehtod.getInstance().addStoryRadioSetPlayCount(hashMap, new Subscriber<Response<ResponseBody>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Response<ResponseBody> o) {

            }
        });
    }

    public void shareAudio(FFlibraryStoryBean.ResultBean item, View view) {
        String share_audio_url = "http://m.fancyedu.com/taleteling/?Id=";
//        final WXShare wxShare = new WXShare();
        String userNickName = "".equals(nickName) ?
                LoginUserSharePrefernce.getName() : nickName;
        final String title = "人美音美心灵美的" + userNickName + "给小朋友们讲了《" + item.getTitle() + "》的故事";
        final String content = "一起为孩子们讲有趣故事，点亮全国孩子的阅读之光";
        final String picture = Constant.IMAGE_URL + item.getBookImg() + ImgFilter.SSCC_H350_W280;
        final String webpageUrl = share_audio_url + item.getSysNo();


        SharePopupWindow sharePopupWindow = new SharePopupWindow(context, view);
        sharePopupWindow.setShareCallBack(new SharePopupWindow.ShareCallBack() {
            @Override
            public void wxCallBack() {
                WXShareUtil.getInstance(context).shareWebPage(picture, title, content, webpageUrl, false);
            }

            @Override
            public void wxFriendCallBack() {
                WXShareUtil.getInstance(context).shareWebPage(picture, title, content, webpageUrl, true);
            }
        });
        sharePopupWindow.showPopupWindow();
    }
}
